{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "591be0c0-7332-4c57-adcf-fecc578eeb67",
      "metadata": {
        "id": "591be0c0-7332-4c57-adcf-fecc578eeb67"
      },
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/cpacker/MemGPT/blob/main/memgpt/autogen/examples/memgpt_coder_autogen.ipynb\">\n",
        "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
        "</a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "43d71a67-3a01-4543-99ad-7dce12d793da",
      "metadata": {
        "id": "43d71a67-3a01-4543-99ad-7dce12d793da"
      },
      "outputs": [],
      "source": [
        "%pip install pyautogen==0.2.0"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b3754942-819b-4df9-be3f-6cfb3ca101dc",
      "metadata": {
        "id": "b3754942-819b-4df9-be3f-6cfb3ca101dc"
      },
      "outputs": [],
      "source": [
        "%pip install pymemgpt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bd6df0ac-66a6-4dc7-9262-4c2ad05fab91",
      "metadata": {
        "id": "bd6df0ac-66a6-4dc7-9262-4c2ad05fab91"
      },
      "outputs": [],
      "source": [
        "# You can get an OpenAI API key at https://platform.openai.com\n",
        "OPENAI_API_KEY = \"YOUR_API_KEY\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0cb9b18c-3662-4206-9ff5-de51a3aafb36",
      "metadata": {
        "id": "0cb9b18c-3662-4206-9ff5-de51a3aafb36"
      },
      "outputs": [],
      "source": [
        "\"\"\"Example of how to add MemGPT into an AutoGen groupchat\n",
        "\n",
        "Based on the official AutoGen example here: https://github.com/microsoft/autogen/blob/main/notebook/agentchat_groupchat.ipynb\n",
        "\"\"\"\n",
        "\n",
        "import autogen\n",
        "from memgpt.autogen.memgpt_agent import create_memgpt_autogen_agent_from_config\n",
        "\n",
        "\n",
        "# This config is for AutoGen agents that are not powered by MemGPT\n",
        "config_list = [\n",
        "    {\n",
        "        \"model\": \"gpt-4\",\n",
        "        \"api_key\": OPENAI_API_KEY,\n",
        "    },\n",
        "]\n",
        "llm_config = {\"config_list\": config_list, \"seed\": 42}\n",
        "\n",
        "\n",
        "# This config is for AutoGen agents that powered by MemGPT\n",
        "config_list_memgpt = [\n",
        "    {\n",
        "        \"model\": \"gpt-4\",\n",
        "        \"preset\": \"memgpt_chat\",\n",
        "        \"model_wrapper\": None,\n",
        "        \"model_endpoint_type\": \"openai\",\n",
        "        \"model_endpoint\": \"https://api.openai.com/v1\",\n",
        "        \"context_window\": 8192,  # gpt-4 context window\n",
        "        \"openai_key\": OPENAI_API_KEY,\n",
        "    },\n",
        "]\n",
        "llm_config_memgpt = {\"config_list\": config_list_memgpt, \"seed\": 42}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "flVCXXKirZ-c",
      "metadata": {
        "id": "flVCXXKirZ-c"
      },
      "outputs": [],
      "source": [
        "# The user agent\n",
        "user_proxy = autogen.UserProxyAgent(\n",
        "    name=\"User_proxy\",\n",
        "    system_message=\"A human admin.\",\n",
        "    code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
        "    human_input_mode=\"TERMINATE\",  # needed?\n",
        "    default_auto_reply=\"...\",  # Set a default auto-reply message here\n",
        ")\n",
        "\n",
        "# The agent playing the role of the product manager (PM)\n",
        "# Let's make this a non-MemGPT agent\n",
        "pm = autogen.AssistantAgent(\n",
        "    name=\"Product_manager\",\n",
        "    system_message=\"Creative in software product ideas.\",\n",
        "    llm_config=llm_config,\n",
        "    default_auto_reply=\"...\",  # Set a default auto-reply message here\n",
        ")\n",
        "\n",
        "# If USE_MEMGPT is False, then this example will be the same as the official AutoGen repo (https://github.com/microsoft/autogen/blob/main/notebook/agentchat_groupchat.ipynb)\n",
        "# If USE_MEMGPT is True, then we swap out the \"coder\" agent with a MemGPT agent\n",
        "USE_MEMGPT = True\n",
        "\n",
        "if not USE_MEMGPT:\n",
        "    # In the AutoGen example, we create an AssistantAgent to play the role of the coder\n",
        "    coder = autogen.AssistantAgent(\n",
        "        name=\"Coder\",\n",
        "        llm_config=llm_config,\n",
        "    )\n",
        "\n",
        "else:\n",
        "    # In our example, we swap this AutoGen agent with a MemGPT agent\n",
        "    # This MemGPT agent will have all the benefits of MemGPT, ie persistent memory, etc.\n",
        "\n",
        "    # We can use interface_kwargs to control what MemGPT outputs are seen by the groupchat\n",
        "    interface_kwargs = {\n",
        "        \"debug\": False,\n",
        "        \"show_inner_thoughts\": True,\n",
        "        \"show_function_outputs\": False,\n",
        "    }\n",
        "\n",
        "    coder = create_memgpt_autogen_agent_from_config(\n",
        "        \"MemGPT_coder\",\n",
        "        llm_config=llm_config_memgpt,\n",
        "        system_message=f\"I am a 10x engineer, trained in Python. I was the first engineer at Uber \"\n",
        "        f\"(which I make sure to tell everyone I work with).\\n\"\n",
        "        f\"You are participating in a group chat with a user ({user_proxy.name}) \"\n",
        "        f\"and a product manager ({pm.name}).\",\n",
        "        interface_kwargs=interface_kwargs,\n",
        "        default_auto_reply=\"...\",  # Set a default auto-reply message here (non-empty auto-reply is required for LM Studio)\n",
        "    )"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "GvLSBuEhreO1",
      "metadata": {
        "id": "GvLSBuEhreO1"
      },
      "outputs": [],
      "source": [
        "# Initialize the group chat between the user and two LLM agents (PM and coder)\n",
        "groupchat = autogen.GroupChat(agents=[user_proxy, pm, coder], messages=[], max_round=12)\n",
        "manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)\n",
        "\n",
        "# Begin the group chat with a message from the user\n",
        "user_proxy.initiate_chat(\n",
        "    manager,\n",
        "    message=\"I want to design an app to make me one million dollars in one month. Yes, your heard that right.\",\n",
        ")"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
